var React = require('react');
var ReactDOM = require('react-dom');

var redux = require('redux');
var createStore = redux.createStore;
var combineReducers = redux.combineReducers;
var redux = require('react-redux')
var Provider = redux.Provider;
var connect = redux.connect;

var ipc = require('electron').ipcRenderer;
var remote = require('electron').remote;
const app = remote.app;
var fs = remote.require("fs");
var path = remote.require('path');
const Console = require('console').Console;

var AppContent = require("./AppContent.js");
var appReducer = require("./reducer");
var config = require('./config.js');
const {crashReporter} = require('electron')

var initState = {
    conversations:[],
    contacts:[],
    contact:{},
    //当前会话
    conversation:{},
    messages:[],
    loginUser:{},
};

var store = createStore(appReducer, initState);

var appContent;
ReactDOM.render(
    <Provider store={store}>
        <AppContent ref={a => appContent=a}/>
    </Provider>,
    document.getElementById('root')
);

ipc.on("exit", function(event, args) {
    appContent.getWrappedInstance().exit();
});


var userDataPath = app.getPath("userData");
var stdout = path.join(userDataPath, 'stdout.log');
var stderr = path.join(userDataPath, 'stderr.log');

const output = fs.createWriteStream(stdout);
const errorOutput = fs.createWriteStream(stderr);

if (!config.DEBUG) {
    console.log("stdout:", stdout, " stderr:", stderr);
    console = new Console(output, errorOutput);
    console.debug = console.log;
}

crashReporter.start({
    submitURL: '',
    uploadToServer: false,
    companyName: '木瓜科技'
});
